#!/usr/bin/env bash
# fake padding for gui windows (terrible)

padding="$(xrq st.borderpx)"
border_color_normal="$(bspc config normal_border_color)"
border_color_active="$(bspc config focused_border_color)"
background_color="$(xrq background)"
border_width="$(bspc config border_width)"

background_color=$(colort -t $background_color)
border_color_normal=$(colort -t $border_color_normal)
border_color_active=$(colort -t $border_color_active)

dec2hex() {
    printf "%X\n" $1
}

pad_window() {
    bspc config -n $2 border_width $((padding + border_width))
    chwb2 -I "$background_color" -i "$padding" \
          -O "$1" -o "$border_width" \
          $2 &
}

declare -A term_map
cached_is_terminal() {
    if [[ -z "${term_map[$1]}" ]]; then
        xprop="$(xprop -id $1)"
        grep -q st-256color <<< "$xprop" && term_map[$1]=true && return
        grep -q Emacs <<< "$xprop" && term_map[$1]=true && return

        term_map[$1]=false
    fi

    return $(iif "${term_map[$1]}" 0 1)
}

non_terminals() {
    color=$1
    shift
    for wid in "$@"; do
        if ! cached_is_terminal $wid; then
            pad_window "$color" "$wid"
        fi
    done
}

draw() {
    non_terminals $border_color_active $(bspc query -N -n .descendant_of.window.leaf.!fullscreen)
    non_terminals $border_color_normal $(bspc query -N -n .!descendant_of.window.leaf.!fullscreen)
}

draw

bspc subscribe node_{geometry,focus} | while read msg; do
    draw
done
